﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Etapa1.ModelDescription
{
    public class Modelo
    {
        public Modelo(List<VertexPositionNormalTexture> listaVertices,List<int> indices)
        {
            for (int i = 0; i < listaVertices.Count; i++)
            {
                vertices.Add(new Vertice(listaVertices[i], i));
            }

            for (int i = 0; i < indices.Count; i+=3)
            {                
                Vertice v0 = vertices[indices[i]];
                Vertice v2 = vertices[indices[i + 1]];
                Vertice v1 = vertices[indices[i + 2]];
                Triangulo newTriangulo = new Triangulo(v0, v1, v2);
                v0.addTriangulo(newTriangulo);
                v1.addTriangulo(newTriangulo);
                v2.addTriangulo(newTriangulo);

                triangulos.Add(newTriangulo);

                Edge[] tempedges = new Edge[] { new Edge(v0, v1), new Edge(v1, v2), new Edge(v2, v0) };

                for (int j = 0; j < 3; j++)
                {
                    Edge edx = edges.Find((a) => a.Vertices[0] == tempedges[j].Vertices[1] && a.Vertices[1] == tempedges[j].Vertices[0] || a.Vertices[0] == tempedges[j].Vertices[0] && a.Vertices[1] == tempedges[j].Vertices[1]);
                    if (edx==null)
                    {
                        tempedges[j].TrianguloPertencente.Add(newTriangulo);
                        tempedges[j].PositionInEdgeList = edges.Count;
                        edges.Add(tempedges[j]);
                        newTriangulo.Edges[j] = tempedges[j];
                        tempedges[j].NotifyVerticesAboutEdge();
                    }
                    else
                    {
                        edx.TrianguloPertencente.Add(newTriangulo);                        
                        newTriangulo.Edges[j] = edx;
                    }    
                }
            }
        }

        public int NumVertices
        {
            get
            {
                return vertices.Count;
            }
        }

        public int NumPrimitivas
        {
            get { return triangulos.Count; }
        }
        protected List<Triangulo> triangulos = new List<Triangulo>();

        public List<Triangulo> Triangulos
        {
            get { return triangulos; }            
        }
        protected List<Vertice> vertices = new List<Vertice>();

        private List<Edge> edges = new List<Edge>();

        public List<Edge> Edges
        {
            get { return edges; }
            set { edges = value; }
        }
            
        public List<Vertice> Vertices
        {
            get { return vertices; }            
        }
        
    }
}
